package com.bw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.domain.SysRole;
import com.bw.domain.SysRoleMenu;
import com.bw.domain.SysUserRole;
import com.bw.service.SysRoleMenuService;
import com.bw.service.SysRoleService;
import com.bw.mapper.SysRoleMapper;
import com.bw.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author THINKAPD
* @description 针对表【sys_role】的数据库操作Service实现
* @createDate 2024-03-04 15:28:13
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{
    @Autowired
    SysRoleMenuService sysRoleMenuService;
    @Autowired
    SysUserRoleService sysUserRoleService;

    @Transactional
    @Override
    public boolean del(Integer id) {
        try {
            //删除关联表 -user-role
            LambdaQueryWrapper<SysUserRole> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SysUserRole::getRoleId,id);
            sysUserRoleService.remove(wrapper1);
            //删除关联表 -role_menu
            LambdaQueryWrapper<SysRoleMenu> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(SysRoleMenu::getRoleId,id);
            sysRoleMenuService.remove(wrapper2);
            //删除自身表
            this.removeById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException("删除失败");
        }

    }
}




